package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.GoodsDetailsActivity;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.bean.SearchItemBeen;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：购物车，零售列表 嵌套商品  适配器
 */
public class ItemCarRetailAdapter extends RecyclerView.Adapter<ItemCarRetailAdapter.MyViewCarRetailHolder> {

    private ItemClickListener mItemClickListener;
    private List<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> mList;
    private Context mContext;
    private CarRetailAdapter carRetailAdapter;
    private final LayoutInflater mLayoutInflater;
    private boolean is_invalid;
    private Dialog dialog;
    private ShopCarListPresenter mPresenter;
    private int one_position;


    public ItemCarRetailAdapter(Context context, List<ShopCarListBeen.DataBean.ValidBean.GoodsBeanX> list, boolean is_invalid, CarRetailAdapter carRetailAdapter, ShopCarListPresenter presenter, int one_position ) {
        this.mList = list;
        this.mContext = context;
        this.carRetailAdapter = carRetailAdapter;
        this.is_invalid = is_invalid;
        this.mPresenter = presenter;
        this.one_position = one_position;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    @NonNull
    @Override
    public MyViewCarRetailHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_car_retail_good, parent, false);
        return new MyViewCarRetailHolder(mContext, inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull MyViewCarRetailHolder holder, int position) {

        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getProductInfo().getImage()).apply(options.transform(new CenterCrop(), roundedCorners)).into(holder.ivShopPicture);
        holder.tvShopGoodsName.setText(mList.get(position).getProductInfo().getStore_name());
        holder.tvGoodsNum.setText(mList.get(position).getCart_num() + "");

        if (Double.parseDouble(mList.get(position).getProductInfo().getOt_price()) == 0) {
            holder.tvShopGoodsPrice.setText("¥" + mList.get(position).getProductInfo().getPrice() + "");
            holder.tvShopGoodsPriceOld.setVisibility(View.GONE);
        } else {
            holder.tvShopGoodsPriceOld.setVisibility(View.VISIBLE);
            holder.tvShopGoodsPriceOld.setText("¥" + mList.get(position).getProductInfo().getPrice());
            holder.tvShopGoodsPrice.setText("¥" + mList.get(position).getProductInfo().getOt_price() + "");
        }
        if (mList.get(position).isIs_select()) {
            mList.get(position).setIs_select(true);
            holder.ivShopSelect.setImageResource(R.drawable.icon_select_orange);
        } else {
            mList.get(position).setIs_select(false);
            holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
        }
        holder.ivShopSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mList.get(position).isIs_select()) {
                    mList.get(position).setIs_select(false);
                    holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
                } else {
                    mList.get(position).setIs_select(true);
                    holder.ivShopSelect.setImageResource(R.drawable.icon_select_orange);
                }
                carRetailAdapter.notifyDataSetChanged();
            }


        });

        if (is_invalid) {
            holder.ivShopSelect.setVisibility(View.INVISIBLE);
            holder.tvGoodsAdd.setVisibility(View.INVISIBLE);
            holder.tvGoodsAddBg.setVisibility(View.INVISIBLE);
            holder.tvGoodsMinus.setVisibility(View.INVISIBLE);
            holder.tvGoodsMinusBg.setVisibility(View.INVISIBLE);
            holder.tvGoodsNum.setVisibility(View.INVISIBLE);
            holder.tvImageHint.setVisibility(View.VISIBLE);
            holder.viewImageBg.setVisibility(View.VISIBLE);

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        } else {
            holder.ivShopSelect.setVisibility(View.VISIBLE);
            holder.tvGoodsAdd.setVisibility(View.VISIBLE);
            holder.tvGoodsAddBg.setVisibility(View.VISIBLE);
            holder.tvGoodsMinus.setVisibility(View.VISIBLE);
            holder.tvGoodsMinusBg.setVisibility(View.VISIBLE);
            holder.tvGoodsNum.setVisibility(View.VISIBLE);
            holder.tvImageHint.setVisibility(View.GONE);
            holder.viewImageBg.setVisibility(View.GONE);
            holder.tvGoodsAddBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        mList.get(position).setIs_select(true);
                        mPresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() + 1, one_position, position);
                    }
                }
            });
            holder.tvGoodsMinusBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Utils.isFastClick()) {
                        mList.get(position).setIs_select(true);
                        if (mList.get(position).getCart_num() > 1) {
                            mPresenter.updateCarNum(mList.get(position).getId() + "", mList.get(position).getCart_num() - 1, one_position, position);
                        } else {
                            dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_delete_this_item, R.string.confirm, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (Utils.isFastClick()) {
                                        mPresenter.removeGood(mList.get(position).getId() + "", one_position, position, false);
                                        dialog.dismiss();
                                    }
                                }
                            });
                            dialog.show();
                        }
                    }
                }
            });


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, GoodsDetailsActivity.class);
                    intent.putExtra("good_id", mList.get(position).getProduct_id() + "");

                    mContext.startActivity(intent);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_delete_this_item, R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Utils.isFastClick()) {
                                mPresenter.removeGood(mList.get(position).getId() + "", one_position, position, false);
                                dialog.dismiss();
                            }
                        }
                    });
                    dialog.show();
                    return true;
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewCarRetailHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_shop_select)
        ImageView ivShopSelect;
        @BindView(R.id.iv_shop_picture)
        ImageView ivShopPicture;
        @BindView(R.id.tv_shop_goods_name)
        TextView tvShopGoodsName;
        @BindView(R.id.tv_shop_goods_price)
        TextView tvShopGoodsPrice;
        @BindView(R.id.tv_shop_goods_price_old)
        TextView tvShopGoodsPriceOld;
        @BindView(R.id.tv_goods_add)
        TextView tvGoodsAdd;
        @BindView(R.id.tv_goods_minus)
        TextView tvGoodsMinus;
        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;
        @BindView(R.id.view_image_bg)
        View viewImageBg;
        @BindView(R.id.tv_image_hint)
        TextView tvImageHint;
        @BindView(R.id.tv_goods_add_bg)
        TextView tvGoodsAddBg;
        @BindView(R.id.tv_goods_minus_bg)
        TextView tvGoodsMinusBg;
        private Context mContext;


        public MyViewCarRetailHolder(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
        }


    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }


}
